/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.controller.buyer.member;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import co.yixiang.cache.Cache;
import co.yixiang.common.annotation.AnonymousAccess;
import co.yixiang.common.annotation.Log;
import co.yixiang.common.annotation.NoRepeatSubmit;
import co.yixiang.common.constant.SystemConfigConstants;
import co.yixiang.common.enums.ResultCode;
import co.yixiang.common.enums.VerificationEnums;
import co.yixiang.common.exception.ServiceException;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.utils.SecurityUtils;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.modules.member.entity.domain.UserAddress;
import co.yixiang.modules.member.entity.domain.YxUser;
import co.yixiang.modules.member.entity.dto.YxUserFrom;
import co.yixiang.modules.member.entity.dto.YxUserIntegralDetailQueryCriteria;
import co.yixiang.modules.member.entity.dto.YxUserQueryCriteria;
import co.yixiang.modules.member.entity.vo.UserAddressVo;
import co.yixiang.modules.member.entity.vo.YxUserVo;
import co.yixiang.modules.member.service.UserAddressService;
import co.yixiang.modules.member.service.YxUserIntegralDetailService;
import co.yixiang.modules.member.service.YxUserService;
import co.yixiang.modules.order.order.service.YxStoreCartService;
import co.yixiang.modules.security.entity.vo.AuthUser;
import co.yixiang.modules.shop.entity.dto.UserMoneyDto;
import co.yixiang.modules.shop.service.YxSystemConfigService;
import co.yixiang.modules.system.entity.domain.User;
import co.yixiang.modules.system.service.SmsUtil;
import co.yixiang.modules.system.serviceimpl.SmsUtilAliImplService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author hupeng
 * @date 2019-10-06
 */
@Api(tags = "商城:会员管理")
@RestController
@RequestMapping("/user")
public class MemberBuyerController {
    @Autowired
    private YxUserService yxUserService;
    @Autowired
    private UserAddressService userAddressService;
    @Autowired
    private YxStoreCartService cartService;
    @Autowired
    private Cache cache;
    @Autowired
    private SmsUtil smsUtil;
    @Autowired
    private YxUserIntegralDetailService integralDetailService;



    @Log("修改用户")
    @ApiOperation(value = "修改用户")
    @PutMapping
    public ResultMessage<String> update(@Validated @RequestBody YxUserFrom resources) {
        yxUserService.updateUser(resources);
        return ResultUtil.success();
    }


    @Log("手机号绑定")
    @ApiOperation(value = "手机号绑定")
    @PutMapping(value = "/binPhone")
    public ResultMessage<YxUserVo> binPhone(@RequestBody YxUserFrom resources) {
        // 验证码-校验
        if (ObjectUtil.isEmpty(resources.getPhone()) || ObjectUtil.isEmpty(resources.getCode()) ) {
            throw new ServiceException(ResultCode.PARAMS_ERROR);
        }
        final boolean b = smsUtil.verifyCode(resources.getPhone(), VerificationEnums.BIN, resources.getPhone(), resources.getCode());
        if (b) {
            final YxUserVo yxUserVo = yxUserService.binPhone(resources);
            return ResultUtil.data( yxUserVo);
        }
        return ResultUtil.error(ResultCode.VERIFICATION_SMS_CHECKED_ERROR);
    }

    @Log("会员信息")
    @ApiOperation(value = "会员信息")
    @GetMapping
    public ResultMessage<YxUserVo> userMessage() {
        // 获取当前用户的信息;
        final AuthUser userDetails = SecurityUtils.getAuthUser();
        final YxUser one = yxUserService.getOne(new LambdaQueryWrapper<YxUser>().eq(YxUser::getUid, userDetails.getUuid()));
        final YxUserVo yxUserVo = BeanUtil.copyProperties(one, YxUserVo.class);
        return ResultUtil.data(yxUserVo);
    }

    @Log("积分明细")
    @ApiOperation(value = "积分明细")
    @GetMapping("/integral")
    public ResultMessage<Map<String, Object>> userIntegral(YxUserIntegralDetailQueryCriteria criteria, Pageable pageable) {
        // 获取当前用户的信息;
        final AuthUser userDetails = SecurityUtils.getAuthUser();
        if (ObjectUtil.isNotEmpty(userDetails.getUuid())) {
            criteria.setUid(userDetails.getUuid());
            final Map<String, Object> stringObjectMap = integralDetailService.queryAll(criteria, pageable);
            return ResultUtil.data(stringObjectMap);
        }
        return ResultUtil.data(null);
    }

    @ApiOperation(value = "初始化购物车")
    @GetMapping("/initCart")
    public ResultMessage<String> autoLogin() {
        cartService.initCart();
        return ResultUtil.success();
    }

    @Log("收货地址列表")
    @ApiOperation(value = "收货地址列表")
    @GetMapping("/address")
    public ResultMessage<List<UserAddressVo>> addressAll() {
        final List<UserAddressVo> userAddressVos = userAddressService.queryListByVo();
        return ResultUtil.data(userAddressVos);
    }
    @Log("新增收货地址")
    @ApiOperation(value = "新增收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "realName", value = "收货人姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "province", value = "省份", paramType = "query"),
            @ApiImplicitParam(name = "city", value = "城市", paramType = "query"),
            @ApiImplicitParam(name = "district", value = "区/县", paramType = "query"),
            @ApiImplicitParam(name = "detail", value = "详情地址", paramType = "query"),
            @ApiImplicitParam(name = "postCode", value = "邮编", paramType = "query"),
            @ApiImplicitParam(name = "longitude", value = "经度", paramType = "query"),
            @ApiImplicitParam(name = "latitude", value = "纬度", paramType = "query"),
            @ApiImplicitParam(name = "isDefault", value = "是否设置为默认地址", paramType = "query")
    })
    @PostMapping("/address")
    public ResultMessage<String> address(@Validated @RequestBody UserAddress resources) {
        userAddressService.saveAddress(resources);
        return ResultUtil.success();
    }
    @Log("修改收货地址")
    @ApiOperation(value = "修改收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "realName", value = "收货人姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "province", value = "省份", paramType = "query"),
            @ApiImplicitParam(name = "city", value = "城市", paramType = "query"),
            @ApiImplicitParam(name = "district", value = "区/县", paramType = "query"),
            @ApiImplicitParam(name = "detail", value = "详情地址", paramType = "query"),
            @ApiImplicitParam(name = "postCode", value = "邮编", paramType = "query"),
            @ApiImplicitParam(name = "longitude", value = "经度", paramType = "query"),
            @ApiImplicitParam(name = "latitude", value = "纬度", paramType = "query"),
            @ApiImplicitParam(name = "isDefault", value = "是否设置为默认地址", paramType = "query")
    })
    @PutMapping("/address")
    public ResultMessage<String> updateAddress(@Validated @RequestBody UserAddress resources) {
        userAddressService.saveOrUpdateAddress(resources);
        return ResultUtil.success();
    }
    @Log("设置默认收货地址")
    @ApiOperation(value = "设置默认收货地址")
    @PutMapping("/address/default/{id}")
    public ResultMessage<UserAddressVo> defaultAddress(@PathVariable Long id) {
        final UserAddressVo userAddress = userAddressService.setDefaultMemberAddress(id);
        return ResultUtil.data(userAddress);
    }

    @Log("删除收货地址")
    @ApiOperation(value = "删除收货地址")
    @DeleteMapping("/address/{id}")
    public ResultMessage<String> deleteAddress(@PathVariable Long id) {
        this.userAddressService.removeById(id);
        return ResultUtil.success();
    }



}
